


//
//  LYLoginViewController.m
//  音伴尔家听宝
//
//  Created by Burgess on 2017/6/14.
//  Copyright © 2017年 Burgess. All rights reserved.
//

#import "LYLoginViewController.h"
#import "LYTabBarController.h"

#import <AFNetworking.h>
#import <MJExtension.h>
#import <UIImage+AFNetworking.h>
#import <UIImageView+AFNetworking.h>

@interface LYLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UITextField *checkText;
@property (weak, nonatomic) IBOutlet UIButton *getCheckCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *checkPhoneBtn;

@end

@implementation LYLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.phoneText resignFirstResponder];
    [self.checkText  resignFirstResponder];
}

//  点击获取验证码按钮
- (IBAction)getCheckCode:(id)sender {
    
    //  textFlied失去第一响应
    [self.phoneText resignFirstResponder];
    [self.checkText  resignFirstResponder];
    
    if (self.phoneText.text == NULL) {
        //  提示要输入手机号码
        
    }else{
        NSDictionary * paramDic = @{@"mobile" : self.phoneText.text};
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:config];
        
        [manager POST:@"http://cute.fcloudclass.com/api.php/User/get_code" parameters:paramDic progress:^(NSProgress * _Nonnull uploadProgress) {
            
        }  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"%@",responseObject[@"result"]);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    }
}

//  点击验证手机按钮
- (IBAction)checkPhone:(id)sender {
    
    //  textFlied失去第一响应
    [self.phoneText resignFirstResponder];
    [self.checkText  resignFirstResponder];
    
    
    if ([self.phoneText.text isEqualToString:@""] || [self.checkText.text isEqualToString:@""]) {
        //  两个框没有填
        LYTabBarController * tabcon = [[LYTabBarController alloc]init];
        [UIApplication sharedApplication].keyWindow.rootViewController = tabcon;
    }else{
        NSDictionary * paramDic = @{
                                    @"code"    :self.checkText.text,
                                    @"mobile" : self.phoneText.text};
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:config];
        
        [manager POST:@"http://cute.fcloudclass.com/api.php/User/user_login" parameters:paramDic progress:^(NSProgress * _Nonnull uploadProgress) {
            
        }  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"%@",responseObject[@"result"]);
            
            if ([responseObject[@"result" ] isEqualToString:@"登陆成功"] ) {
                LYTabBarController * tabcon = [[LYTabBarController alloc]init];
                [UIApplication sharedApplication].keyWindow.rootViewController = tabcon;
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];

    }
    NSUserDefaults * defaults        =   [NSUserDefaults standardUserDefaults];
    [defaults setValue:self.phoneText.text forKey:@"userName"];

}


@end
